import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: TextFieldPage(),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class TextFieldPage extends StatefulWidget {
  TextFieldPage({Key? key}) : super(key: key);

  @override
  _TextFieldPageState createState() => _TextFieldPageState();
}

class _TextFieldPageState extends State<TextFieldPage> {
  //通过TextEditingController获取TextFiled里的内容
  var username = TextEditingController();

  @override
  void initState() {
    super.initState();
    username.text = 'your account';
  }

  @override
  void dispose() {
    super.dispose();
    username.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TextField Page'),
      ),
      body: Padding(
        child: Column(
          children: [
            TextField(
              decoration: InputDecoration(
                //设置了controller该设置不生效
                //hintText: "user name",
                labelText: "用户",
                icon: Icon(Icons.account_box),
              ),
              controller: username,
              //TextField内的内容发生变化会调用
              onChanged: (value) {
                print(value);
              },
            ),
            TextField(
              obscureText: true,
              decoration: InputDecoration(
                  labelText: "密码",
                  icon: Icon(Icons.lock),
                  hintText: "password",
                  border: OutlineInputBorder()),
            ),
            SizedBox(
              height: 20,
            ),
            Container(
              //标识continer的宽度自适应
              width: double.infinity,
              //padding用来缩小Button的大小
              child: Padding(
                  child: ElevatedButton(
                    child: Text("登录"),
                    onPressed: () {
                      print(username.text);
                    },
                  ),
                  padding: EdgeInsets.fromLTRB(100, 10, 100, 10)),
            ),
            Divider(),
            SizedBox(
              height: 20,
            ),
            TextField(
              decoration: InputDecoration(
                  hintText: "single line", border: OutlineInputBorder()),
            ),
            SizedBox(
              height: 20,
            ),
            TextField(
              maxLines: 3,
              decoration: InputDecoration(
                  hintText: "multipy lines", border: OutlineInputBorder()),
            ),
            SizedBox(
              height: 20,
            ),
          ],
        ),
        padding: EdgeInsets.all(20),
      ),
    );
  }
}
